﻿using NLog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPP.Utils.Services {
    /// <summary>
    /// Helper Class to load and save application/user settings.
    /// 
    /// <list type="bullet">
    /// <item>
    /// <term>Author</term>
    /// <description>Sebastian Will</description>
    /// </item>
    /// </list>
    /// 
    /// </summary> 
    public class SettingsHandler {

        #region Fields

        private static WebConfigEncryption _webConfigEncryption = new WebConfigEncryption();
        private static readonly Logger _logger = LogManager.GetLogger("SettingsHandler");

        #endregion


        #region Methods

        /// <summary>
        /// Get the culture setting from the application settings.
        /// </summary>
        /// <returns>CultureInfo</returns>
        public static CultureInfo GetCultureInfo() {
            return new CultureInfo(Properties.Settings.Default.Culture);
        }

        /// <summary>
        /// Get the culture setting from the application settings.
        /// </summary>
        /// <returns>Culture</returns>
        public static string GetCulture() {
            return Properties.Settings.Default.Culture;            
        }

        /// <summary>
        /// Get the printer page orientation setting from the application settings.
        /// </summary>
        /// <returns>Culture</returns>
        public static string GetPrinterPageOrientation() {
            return Properties.Settings.Default.PrinterPageOrientation;            
        }

        /// <summary>
        /// Get the selected printer setting from the application settings.
        /// </summary>
        /// <returns>Culture</returns>
        public static string GetPrinterSelectedPrinter() {
            return Properties.Settings.Default.PrinterSelectedPrinter;            
        }

        /// <summary>
        /// Get the selected paper size setting from the application settings.
        /// </summary>
        /// <returns>Culture</returns>
        public static string GetPrinterSelectedPaperSize() {
            return Properties.Settings.Default.PrinterSelectedPaperSize;
        }
        

        /// <summary>
        /// Set a AppSetting Parameter.
        /// </summary>
        /// <param name="settingsName">Parameter name</param>
        /// <param name="value">Value</param>
        /// <returns>true/false</returns>
        public static bool SetAppSettingParameter(string settingsName, string value) {
            try {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings[settingsName].Value = value;
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch (Exception ex) {
                _logger.ErrorException("SetAppSettingParameter Error.",ex);
                return false;
            }
        }

        /// <summary>
        /// Set a AppSetting Parameter.
        /// </summary>
        /// <param name="settingsName">Parameter name</param>
        /// <param name="value">Value</param>
        /// <returns>true/false</returns>
        public static bool SetUserSettingParameter(string settingsName, string value) {
            try {
                Properties.Settings.Default.PropertyValues[settingsName].PropertyValue = value;
                Properties.Settings.Default.Save();
                return true;
            }
            catch (Exception ex) {
                _logger.ErrorException("SetUserSettingParameter Error.", ex);
                return false;
            }
        }  

        #endregion
    }
}
